//
//  HLNetWork.m
//  HLMusicBox
//
//  Created by hulinhu on 2017/12/27.
//  Copyright © 2017年 hulinhu. All rights reserved.
//

#import "HLNetWork.h"
#import "HLBaseNetWork.h"

@implementation HLNetWork

#pragma mark
#pragma mark - get post请求
+ (void)requestNetWorkURL:(NSString*)url
                   parmas:(NSDictionary*)parmas
       cacheTimeInSeconds:(NSInteger)cacheTime
               completion:(completionHandle)completion
                   failed:(failedHandle)failedBlock {
    
    HLBaseNetWork *netWork = [[HLBaseNetWork alloc] initWithRequestUrl:url parmas:parmas cacheTime:cacheTime];
    [netWork startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"requestUrl -- %@ \n responseJSONObject-- :%@",request.requestUrl, request.responseJSONObject);
         completion(request.responseJSONObject);
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
         failedBlock(request.error);
    }];
    
}

#pragma mark
#pragma mark - 先加载缓存后请求
+ (void)requestFromCacheURL:(NSString *)url
                     parmas:(NSDictionary *)parmas
                 completion:(completionHandle)completion
                     failed:(failedHandle)failedBlock {
    
    HLBaseNetWork *netWork = [[HLBaseNetWork alloc] initWithRequestUrl:url parmas:parmas cacheTime:1000];
    netWork.ignoreCache = YES;
    
    if ([netWork respondsToSelector:NSSelectorFromString(@"loadCacheData")]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        if ([netWork performSelector:NSSelectorFromString(@"loadCacheData")])
#pragma clang diagnostic pop
        {
            completion(netWork.responseJSONObject);
        }
    }
    
   [netWork startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        completion(request.responseJSONObject);
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        failedBlock(request.error);
    }];
    
}

#pragma mark
#pragma mark - 图片上传
+ (void)uploadImageNetWorkURL:(NSString *)url
                      paramas:(NSDictionary *)parmas
                        image:(UIImage *)image
                   completion:(completionHandle)completion
                       failed:(failedHandle)failedBlock {
    
     HLBaseNetWork *netWork = [[HLBaseNetWork alloc] initWithRequestUrl:url parmas:parmas  upImage:image];
    
    [netWork startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        completion(request.responseJSONObject);
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        failedBlock(request.responseJSONObject);
    }];
    
}

@end
